package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.CheckItem;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    //注入mapper
    @Autowired
    private CheckItemMapper checkItemMapper;

    //添加项
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }

    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page =  checkItemMapper.selectByCondition(queryString);
        long total = page.getTotal();
        List<CheckItem> result = page.getResult();
        return new PageResult(total, result);
    }

    //删除
    public void delete(Integer id) {
        if (checkItemMapper.findByCheckItemId(id) > 0){
            throw new RuntimeException("存在外键约束,删除失败...");
        }
        checkItemMapper.delete(id);
    }

    //根据id查询
    public CheckItem findById(Integer id) {
        return checkItemMapper.findById(id);
    }

    //编辑检查项
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> list = checkItemMapper.findAll();
        return list;
    }
}
